from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU


class SKUSerialzier(serializers.ModelSerializer):
    """
        sku
    """

    class Meta:
        # 指定根据那个模型类生成序列化器字段
        model = SKU
        # 指定那些字段生成
        fields = ('name','default_image')

class OrderGoodsSerialzier(serializers.ModelSerializer):
    """
        订单商品表
    """

    sku=SKUSerialzier()
    class Meta:
        # 指定根据那个模型类生成序列化器字段
        model = OrderGoods
        # 指定那些字段生成
        fields = ('count','price','sku')


class OrderSerializer(serializers.ModelSerializer):
    """
        订单表序列化
    """
    # 关联嵌套序列化返回 USer
    user = serializers.StringRelatedField(read_only=True)
    address = serializers.PrimaryKeyRelatedField(read_only=True)

    # 订单商品表关联订单表
    skus=OrderGoodsSerialzier(many=True)

    class Meta:
        # 指定根据那个模型类生成序列化器字段
        model = OrderInfo
        # 指定那些字段生成

        fields = "__all__"

